<?php

function view($array) {
    if ( LIVE == true ) return false;
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}
function create_javascript_var($key, $value){
        echo '<script type="text/javascript">';
        echo 'var '.$key.' = '.json_encode($value).';';
        echo '</script>';
}


function get_weeknum($time_string = null){
    global $WEEK_ORIGIN;
    if(!($time_string)) $time_string = time();
    $weeks = $time_string - $WEEK_ORIGIN;
    return ceil ( $weeks / (86400*7));
}

function get_date_from_weeknum($weeks) {
    global $WEEK_ORIGIN;
    $time_string = ($weeks * (86400*7)) + $WEEK_ORIGIN;
    return $time_string;
}

function redirect_to ( $url ) {
    if ( $url != NULL ) {
        header("Location: {$url}");
        exit;
    }
}
function is_admin() {
    global $session;
    return ( $session->table_name == 'admn' ) ? true : false;
}
function __autoload($class_name) {
    $class_name = strtolower($class_name);
    $path = "../includes/" . $class_name . ".php";
    if (file_exists($path)) {
        require_once($path);
    } else {
        die("The file {$class_name}.php could not be found.");
    }
}

function count_class_attr($objects, $obj_key, $obj_val) {
    $count = 0;
    foreach ($objects as $object ) {
        if (property_exists($object, $obj_key)) {
           if($object->$obj_key == $obj_val) { $count ++; }
        }
    }
    return $count;
}



function get_class_unique_values($objects, $obj_key) {
    $filtered = array();
    foreach ($objects as $object ) {
       $filtered[] = $object->$obj_key;
    }
    $filtered = array_unique($filtered);
    sort($filtered);
    return($filtered);
}



function count_expiring($objects) {
    $count = 0;
        foreach ($objects as $object ) {
            if ( $object->is_expiring() ) {
                $count ++;
            }
        }
    return $count;
}

function in_array_r($needle, $haystack) {
    $found = false;
    foreach ($haystack as $item) {
        
    if ($item === $needle) { 
            $found = true; 
            break; 
        } elseif (is_array($item)) {
            $found = in_array_r($needle, $item); 
            if($found) { 
                break; 
            } 
        }    
    }
    
    return $found;
}

/**
 * Outputs an object or array in a readable form.
 *
 * @return void
 * @param $object = the object to display; Typically a class, or array.
 * @author epstudios
 */
function prettify( $string ) {
    return ucwords( str_replace("_"," ",$string) );
}
function httpify( $url ) {

    if(substr($url, 0, 4) != "http") {
        return 'http://' . $url;
    }
    return $url;
}

function convert_bytes_to($size) {
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
?>